package com.qbw.app.module.home.holder;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.qbw.app.R;
import com.qbw.app.databinding.HomeHolderBatteryMilleageBinding;
import com.qbw.app.manager.UnitManager;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.VehicleHomeInfo;
import com.qbw.app.model.Voltage;
import com.qbw.app.module.common.holder.CommonHolder;
import com.qbw.app.module.vehicle.VehicleDataBindingAdapter;
import com.qbw.app.module.vehicle.activity.VehicleEstimateDistanceActivity;

/**
 * @author xiansx
 * @date 2020/10/20
 */
public class HomeBatteryMilleageHolder extends CommonHolder<HomeBatteryMilleageHolder.Data, HomeHolderBatteryMilleageBinding> {

    public HomeBatteryMilleageHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.home_holder_battery_milleage, viewParent);
    }

    @Override
    public void bindData(int position, Data data) {
        super.bindData(position, data);
        mBinding.tvKm.setText(UnitManager.getInstance().isUseKm() ? "km" : "mile");
        updateResidualFuelUi(mData.getHomeInfo().getResidualFuel());
        updateMileagelUi(mData.getHomeInfo().getMileage());
        updateVisible(mData.getHomeInfo().getVehicle());
        itemView.setOnClickListener(v -> {
            if (mBinding.clMilleage.getVisibility() != View.VISIBLE) {
                return;
            }
            Vehicle vehicle = mData.getHomeInfo().getVehicle();
            if (vehicle != null) {
                VehicleEstimateDistanceActivity.start(mContext, vehicle);
            }
        });
    }

    /**
     * 电池电量
     */
    private void updateResidualFuelUi(VehicleHomeInfo.ResidualFuel fuel) {
        mBinding.tvNumber.setTextColor(mContext.getResources().getColor(R.color.nm_normal));
        int ipercent = fuel == null ? 0 : (int) fuel.getValue();
        if (fuel == null || (fuel.getState() + "").equals(Voltage.NO_DATA)) {
            mBinding.tvNumber.setText(R.string.na);
            mBinding.ivBattery.setImageResource(R.drawable.nm_icon_battery_disable);
        } else {
            if(ipercent > 100) {
                ipercent = 100;
            }
            mBinding.tvNumber.setText(String.valueOf(ipercent));
            if(ipercent >= 10) {
                mBinding.ivBattery.setImageResource(R.drawable.nm_icon_battery_normal);
            } else {
                mBinding.tvNumber.setTextColor(mContext.getResources().getColor(R.color.nm_exception));
                mBinding.ivBattery.setImageResource(R.drawable.nm_icon_battery_low);
            }
        }
        mBinding.pbBattery.setProgress(ipercent);
        mBinding.pbMilleage.setProgress(ipercent);
    }

    /**
     * 预估里程
     */
    private void updateMileagelUi(VehicleHomeInfo.Mileage mileage) {
        if (mileage == null || (mileage.getState() + "").equals(Voltage.NO_DATA)) {
            mBinding.tvNumberMilleage.setText(R.string.na);
        } else {
            VehicleDataBindingAdapter.setDistanceValue(mBinding.tvNumberMilleage,
                                                       mileage.getValue());
        }
    }

    private void updateVisible(Vehicle vehicle) {
        //是否显示剩余油量，如果剩余油量不显示，那么预估里程默认也不显示
        boolean showOilRemainder = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_OIL_REMAINDER);
        //是否显示预估里程
        boolean showLicheng = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_ESTIMATEDMILEAGE);
        if (!showOilRemainder) {
            showLicheng = false;
        }
        mBinding.clBattery.setVisibility(showOilRemainder ? View.VISIBLE : View.GONE);
        mBinding.clMilleage.setVisibility(showLicheng ? View.VISIBLE : View.GONE);
    }

    public static class Data {
        private VehicleHomeInfo homeInfo;

        public Data(VehicleHomeInfo homeInfo) {
            this.homeInfo = homeInfo;
        }

        public VehicleHomeInfo getHomeInfo() {
            return homeInfo;
        }

        public void setHomeInfo(VehicleHomeInfo homeInfo) {
            this.homeInfo = homeInfo;
        }
    }

    public static boolean canShow(Vehicle vehicle) {
        //是否显示剩余油量，如果剩余油量不显示，那么预估里程默认也不显示
        boolean showOilRemainder = vehicle.hasMenuId(Vehicle.MENU_ID_OIL_REMAINDER);
        //是否显示预估里程
        boolean showLicheng = vehicle.hasMenuId(Vehicle.MENU_ID_ESTIMATEDMILEAGE);
        if (!showOilRemainder) {
            showLicheng = false;
        }
        return showOilRemainder || showLicheng;
    }
}
